//
//  MainVC.m
//  MyDemo
//
//  Created by jay on 2020/8/22.
//  Copyright © 2020 jay. All rights reserved.
//

#import "MainVC.h"
#import "MainVM.h"
#import "MainCell.h"
#import "HistoryVC.h"

//定时请求接口时间间隔(5分钟)
#define kRefreshTimeInterval    300

@interface MainVC ()

@property (nonatomic, strong) MainVM *curVM;

@end

@implementation MainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"当前数据";
    
    [self setNavRightItems];
    
    self.myTableView.backgroundColor = TX_COLOR_FROM_RGB(0xFAFAFA);
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.myTableView registerClass:[MainCell class] forCellReuseIdentifier:kTableCellID];
        
    TX_WS(weakSelf);
    
    // 集成下拉刷新
    [self setHeaderRereshingBlock:^{
        
        [weakSelf reqGetData:YES];
        
    } kRefreshingHeaderClass];
    
    //请求数据
    [self reqGetData:NO];
    
    //定时刷新数据
    NSTimer *timer = [NSTimer timerWithTimeInterval:kRefreshTimeInterval target:self selector:@selector(_handleRefreshTimer) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

#pragma mark - Help Method

//设置导航栏按钮
-(void)setNavRightItems {
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"调用记录" style:UIBarButtonItemStylePlain target:self action:@selector(recordAction)];
    self.navigationItem.rightBarButtonItem = rightBtn;
}

#pragma mark - Button Action

//按钮事件响应
-(void)recordAction {
    
    HistoryVC *vc = [[HistoryVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Request Method

//请求数据
-(void)reqGetData:(BOOL )isFromRefrehing {
    
    if (!isFromRefrehing) {
        [self showLoading];
    }
    
    TX_WS(weakSelf);
    [self.curVM getDataFromNetCompleteHandle:^(NSError *error) {
        
        if (isFromRefrehing) {
            [weakSelf headerEndRefreshing];
        } else {
            [self hideLoading];
        }
        
        //先隐藏空状态视图
        [self hideEmptyView];
        
        [weakSelf.myTableView reloadData];

        if (!self.curVM.curModel) {
            [self showEmptyImage:@"no_content" title:error?error.localizedDescription:@"暂无数据" buttonName:@"" inView:self.myTableView buttonBlock:^{
            }];
        } else {
            if (error) {
                TX_TOAST(error.localizedDescription);
            }
        }
    }];
}

#pragma mark - UITableViewDataSource & UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.curVM rowNumber];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MainCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableCellID];
    
    cell.titleLabel.text = [self.curVM propertyNameForRow:indexPath.row];
    
    cell.contentLabel.text = [self.curVM propertyValueForRow:indexPath.row];

    return cell;
}

#pragma mark - Timer

//timer处理器
-(void)_handleRefreshTimer {
    
    //刷新数据
    [self reqGetData:NO];
}

#pragma mark - Getter

- (MainVM *)curVM {
    if(_curVM == nil) {
        _curVM = [[MainVM alloc] init];
    }
    return _curVM;
}

@end
